home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11489 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What happens on realloc() failure?
  5. Date: Sun, 24 Mar 96 17:34:06 GMT
  6. Organization: none
  7. Message-ID: <827688846snz@genesis.demon.co.uk>
  8. References: <Pine.SOL.3.91-941213.960322192958.24127A-100000@aidan.ncl.ac.uk> <4ivnfiINNdib@keats.ugrad.cs.ubc.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4ivnfiINNdib@keats.ugrad.cs.ubc.ca>
  15.            c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
  16.  
  17. >In article <Pine.SOL.3.91-941213.960322192958.24127A-100000@aidan.ncl.ac.uk>,
  18. >Tom Seddon  <T.W.Seddon@ncl.ac.uk> wrote:
  19. > >I am writing a program which will need to call realloc() a fair amount. 
  20. > >What I would like to know is whether the original data is still usable if
  21. > >the realloc() fails. (Although it is unlikely that there will be problems
  22. > >with running out of memory, it is possible that there will be problems due
  23. > >to fragmentation of memory.)
  24. >
  25. >Read some reliable documentation (ISO standard, K&R2). If you had the K&R, you
  26. >would know from page 252 that:
  27. >
  28. >        realloc returns a pointer to the new space, or NULL if the
  29. >        request cannot be satisfied in which case [the pointer parameter]
  30. >        is not changed.
  31.  
  32. I assume you meant by [the pointer parameter] the object that the pointer
  33. argument pointed to.
  34.  
  35. > >Since realloc() returns NULL on failure, is it safe to use the pointer to 
  36. > >the malloc()ed area I was trying to resize?
  37. >
  38. >Apparently so.
  39.  
  40. For a more confident statement the standard itself says:
  41.  
  42. "If the space cannot be allocated, the object pointed to by ptr is unchanged."
  43.  
  44. >
  45. > >Or will the old area have 
  46. > >been freed by free() and now be unusable? What does the ANSI standard 
  47. > >have to say about this? I've never seen anything about this point before 
  48.  
  49. When realloc fails the old object still exists and you should still
  50. free it when you have finished with it. So simply ignoring it after realloc
  51. fails is essentially a memory leak.
  52.  
  53. >Presumably what I just said, since the K&R2 is quite faithful to the standard.
  54.  
  55. And now we have the proof (at least in this case)! :-)
  56.  
  57. -- 
  58. -----------------------------------------
  59. Lawrence Kirby | fred@genesis.demon.co.uk
  60. Wilts, England | 70734.126@compuserve.com
  61. -----------------------------------------
  62.